package com.xyw.code.platform.webapps.util;

import org.aspectj.lang.JoinPoint;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.reflect.Method;

/**
 * Created by xuyiwei on 2019/8/12.
 */
public class LogUtil {

    /***
     * 获取操作信息
     * @param point
     * @return
     */
    public static String getControllerMethodDescription(JoinPoint point) throws Exception {
        String description = "";
         //获取方法名 再通过类下面的方法得到具体是哪个方法 最后通过方法得到描述
        String methodName = point.getSignature().getName();
        Class className = point.getTarget().getClass();
        //获取连接点参数
        Object[] args = point.getArgs();
        //得到该类下面的方法
        Method[] methods = className.getMethods();
        for(Method method:methods){
            if(method.getName().equals(methodName)){
                //区分同名方法
                if(method.getParameters().length==args.length){
                    description = method.getAnnotation(com.xyw.code.platform.webapps.log.SysLog .class).descrption();
                }
            }
        }
        return description;
    }


    /**
     * 获取堆栈信息
     * @param throwable
     * @return
     */
    public static String getStackTrace(Throwable throwable){
        StringWriter sw = new StringWriter();
        try (PrintWriter pw = new PrintWriter(sw)) {
            throwable.printStackTrace(pw);
            return sw.toString();
        }
    }
}
